//
//  ZLPullMenuCell.m
//  ZLPullMenu
//
//  Created by 科大 on 2024/5/27.
//

#import "ZLPullMenuCell.h"
#import <Masonry/Masonry.h>

@interface ZLPullMenuCell()
/// 标题
@property (nonatomic, strong) UIButton *titleButton;
@end

@implementation ZLPullMenuCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self.contentView addSubview:self.titleButton];
        [self.titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
    }
    return self;
}

- (void)setItem:(ZLPullMenuItem *)item {
    _item = item;
    if (item.selected) {
        self.titleButton.titleLabel.font = item.config.selectedFont;
        [self.titleButton setTitleColor:item.config.textSelectedColor forState:UIControlStateNormal];
        UIImage *image = [[self imageNamed:@"arrow_up"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        self.titleButton.imageView.tintColor = item.config.arrowSelectedColor;
        [self.titleButton setImage:image forState:UIControlStateNormal];
    } else {
        self.titleButton.titleLabel.font = item.config.font;
        [self.titleButton setTitleColor:item.config.textColor forState:UIControlStateNormal];
        UIImage *image = [[self imageNamed:@"arrow_down"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        self.titleButton.imageView.tintColor = item.config.arrowColor;
        [self.titleButton setImage:image forState:UIControlStateNormal];
    }
    [self.titleButton setTitle:item.menuTitle forState:UIControlStateNormal];
    [self.titleButton setTitleEdgeInsets:UIEdgeInsetsMake(0, - self.titleButton.imageView.image.size.width, 0, self.titleButton.imageView.image.size.width)];
    [self.titleButton setImageEdgeInsets:UIEdgeInsetsMake(0, self.titleButton.titleLabel.bounds.size.width, 0, -self.titleButton.titleLabel.bounds.size.width)];
}

#pragma mark - init

- (UIButton *)titleButton {
    if (!_titleButton) {
        _titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _titleButton.contentMode = UIViewContentModeRight;
        [_titleButton setTitleColor:[UIColor colorWithWhite:0.4 alpha:1] forState:UIControlStateNormal];
        UIImage *image = [[self imageNamed:@"arrow_down"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [_titleButton setImage:image forState:UIControlStateNormal];
        _titleButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _titleButton.userInteractionEnabled = NO;
    }
    return _titleButton;
}

- (UIImage *)imageNamed:(NSString *)name {
    NSBundle *frameworkBundle = [NSBundle bundleForClass:[self class]];
    NSBundle *imageBundle = [NSBundle bundleWithPath:[frameworkBundle pathForResource:@"ZLPullMenu" ofType:@"bundle"]];
    UIImage *image = [UIImage imageNamed:name inBundle:imageBundle compatibleWithTraitCollection:nil];
    if (image == nil) {
        return [[UIImage alloc] init];
    }
    return image;
}

@end
